package com.hean.activity;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.hean.util.Action;

public class MyMenu extends Activity {
	private static final int MSG_UPDATE = 1;
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == MSG_UPDATE) {
				Toast.makeText(MyMenu.this, "更新完成!", 2000).show();
			}
		}
	};
	private String[] menuOptions = { "新建", "保存", "更新" ,"多选"};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		for (int i = 0; i < menuOptions.length; i++) {
			menu.add(0, i, i, menuOptions[i]);
		}
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int itemId = item.getItemId();
		if (itemId == 0) {
			new CreateAction().execute();
		} else if (itemId == 1) {
			new SaveAction().execute();
		} else if (itemId == 2) {
			//弹出式菜单
			new UpdateAction().execute();
		}else if(itemId==3){
			//多选
			Builder builder=new AlertDialog.Builder(MyMenu.this);
			builder.setTitle("多选");
			builder.setMultiChoiceItems(new String[]{"一","二","三"}, null, new OnMultiChoiceClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which, boolean isChecked) {
					
				}
			});
			builder.setPositiveButton("ok",new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			});
			builder.setNegativeButton("cancel", null);
			AlertDialog dialog=builder.create();
			dialog.show();
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	private class CreateAction implements Action {
		@Override
		public void execute() {
			// create
			Builder builder = new AlertDialog.Builder(MyMenu.this);
			builder.setTitle("确定一下");
			// 1
			builder.setMessage("真的要新建吗?");
			builder.setPositiveButton("确定", new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Toast.makeText(MyMenu.this, "新建了", 2000).show();
				}
			});
			builder.setNegativeButton("取消", new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
				}
			});
			// builder.setNeutralButton(arg0, arg1)

			AlertDialog dialog = builder.create();
			dialog.show();
		}
	}

	/**
	 * 
	 * @author steven
	 * 
	 */
	private class SaveAction implements Action {
		int position = 0;
		String[] singleOptions = { "One", "Two", "Three" };

		@Override
		public void execute() {
			// save
			Builder builder = new AlertDialog.Builder(MyMenu.this);
			builder.setTitle("请选择: ");
			// 2
			// builder.setMessage("真的要新建吗?");

			builder.setSingleChoiceItems(singleOptions, 0,
					new OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							position = which;
						}
					});

			builder.setPositiveButton("确定", new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Toast.makeText(MyMenu.this,
							"你选择的是: " + singleOptions[position], 2000).show();
				}
			});

			builder.setNegativeButton("取消", new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
				}
			});
			// builder.setNeutralButton(arg0, arg1)

			AlertDialog dialog = builder.create();
			dialog.show();
		}
	}

	/**
	 * 
	 * @author steven
	 * 
	 */
	private class UpdateAction implements Action {
		String[] itemOptions = { "One", "Two", "Three" };
		ProgressDialog pd;

		@Override
		public void execute() {
			// update
			Builder builder = new AlertDialog.Builder(MyMenu.this);
			builder.setTitle("更新哪个?");
			builder.setItems(itemOptions, new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					pd = new ProgressDialog(MyMenu.this);
					pd.setTitle("正在更新");
					pd.setMessage("一会就好...");
					pd.show();
					new Thread() {
						@Override
						public void run() {
							try {
								Thread.sleep(5000);
								pd.dismiss();
								Message msg = new Message();
								msg.what = MSG_UPDATE;
								handler.sendMessage(msg);

							} catch (InterruptedException e) {
							}
						}
					}.start();
				}
			});
			AlertDialog dialog = builder.create();
			dialog.show();
		}
	}
}
